package visualization.components;

import java.awt.Color;
import java.util.HashSet;

/**
 * Generates colors for a Visualization.
 * @author Ben Schwab
 *
 */
public class ColorGenerator {
    private static final int COLOR_RANGE = 256;

    private Color[] myColors;
    private int myNumColors;
    private HashSet<Color> myRandomColors;

    /**
     * @param numColors number of colors to generate
     */
    public ColorGenerator(int numColors) {
        myNumColors = numColors;
        myRandomColors = new HashSet<Color>();
        myColors = new Color[numColors];
    }

    /**
     * Generate an array of random colors.
     * @return the array
     */
    public Color[] generateRandomColors() {
        int position = 0;
        myRandomColors.clear();
        while (myRandomColors.size() < myNumColors) {
            int red = (int) (Math.random() * COLOR_RANGE);
            int green = (int) (Math.random() * COLOR_RANGE);
            int blue = (int) (Math.random() * COLOR_RANGE);
            Color newColor = new Color(red, green, blue);
            if (myRandomColors.add(newColor)) {
                myColors[position] = newColor;
                position++;
            }
        }
        return myColors;
    }
}
